/**
 * 2013 2013-5-9
 * @author asfcxvcxcvb
 */
package com.surfilter.dp.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.surfilter.dp.constant.Constants;
import com.surfilter.dp.context.ContextHolder;
import com.surfilter.dp.model.ModuleModel;
import com.surfilter.dp.util.ViewUtil;

/**
 * @author asfcxvcxcvb
 * 
 */
@SuppressWarnings("serial")
public class ModuleAddDialog extends JDialog {

	private JPanel contentPane;

	private JTextField textField_moduleName;
	private JTextField textField_moduleText;
	private JTextField textField_pname;
	private JComboBox textField_moduleType;
	private JTextField textField_serviceName;
	private JTextField textField_remark;
	private JTextField textField_codePath;
	private JTextField textField_serviceCls;
	private JTextField textField_tableName;
	private JSpinner textField_sortIndex;
	private JButton btn_saveInfo;
	private JButton btn_delModule;

	private ModuleModel parent;

	/**
	 * Create the frame.
	 */
	public ModuleAddDialog(ModuleModel parent, JFrame parentFrame) {
		super(parentFrame);
		this.parent = parent;
		initView();
		initData();
		initListeners();
	}

	/**
	 * 
	 */
	private void initListeners() {
		final ModuleAddDialog dialog = this;
		btn_saveInfo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModuleModel model = getFormData();
				ContextHolder context = ContextHolder.getInstance();
				context.getModuleDao().addNewModule(model);
				context.getModuleTree().addNewNode(model);
				JOptionPane.showMessageDialog(context.getMainWin(), "添加模块成功");
				context.getModuleTree().initData();
				dialog.dispose();
			}
		});
		btn_delModule.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
	}

	private ModuleModel getFormData() {
		ModuleModel model = new ModuleModel();
		model.setModuleName(textField_moduleName.getText());
		model.setModuleText(textField_moduleText.getText());
		model.setPname(textField_pname.getText());
		model.setModuleType(textField_moduleType.getSelectedItem().equals(
				"目录模块") ? Constants.TYPE_FOLDER : Constants.TYPE_FUN);
		model.setServiceName(textField_serviceName.getText());
		model.setRemark(textField_remark.getText());
		model.setCodePath(textField_codePath.getText());
		model.setServiceCls(textField_serviceCls.getText());
		model.setTableName(textField_tableName.getText());
		model.setSortIndex((Integer) textField_sortIndex.getValue());
		return model;
	}

	/**
	 * 
	 */
	private void initData() {
		if (this.parent != null) {
			textField_pname.setText(parent.getModuleName());
			textField_pname.setEditable(false);
			textField_sortIndex.setValue(0);
		}
	}

	private void initView() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 359);
		this.setModal(true);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		JLabel lblNewLabel = new JLabel("模块名称");
		lblNewLabel.setBounds(12, 42, 73, 15);
		contentPane.add(lblNewLabel);

		textField_moduleName = new JTextField();
		textField_moduleName.setBounds(88, 39, 125, 21);
		contentPane.add(textField_moduleName);
		textField_moduleName.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("展示名称");
		lblNewLabel_1.setBounds(223, 42, 66, 15);
		contentPane.add(lblNewLabel_1);

		textField_moduleText = new JTextField();
		textField_moduleText.setBounds(295, 39, 137, 21);
		contentPane.add(textField_moduleText);
		textField_moduleText.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("父模块");
		lblNewLabel_2.setBounds(12, 70, 73, 15);
		contentPane.add(lblNewLabel_2);

		textField_pname = new JTextField();
		textField_pname.setBounds(88, 67, 125, 21);
		contentPane.add(textField_pname);
		textField_pname.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("模块类型");
		lblNewLabel_3.setBounds(223, 70, 66, 15);
		contentPane.add(lblNewLabel_3);

		textField_moduleType = new JComboBox(new Object[] { "目录模块", "功能模块" });
		textField_moduleType.setBounds(295, 67, 137, 21);
		contentPane.add(textField_moduleType);

		JLabel lblNewLabel_4 = new JLabel("Java服务类");
		lblNewLabel_4.setBounds(12, 98, 73, 15);
		contentPane.add(lblNewLabel_4);

		textField_serviceName = new JTextField();
		textField_serviceName.setBounds(88, 98, 125, 21);
		textField_serviceName.setColumns(10);
		contentPane.add(textField_serviceName);

		JLabel lblJs = new JLabel("JS服务类");
		lblJs.setBounds(223, 98, 66, 15);
		contentPane.add(lblJs);

		textField_serviceCls = new JTextField();
		textField_serviceCls.setBounds(295, 95, 137, 21);
		textField_serviceCls.setColumns(10);
		contentPane.add(textField_serviceCls);

		JLabel label_1 = new JLabel("模块备注");
		label_1.setBounds(12, 126, 73, 15);
		contentPane.add(label_1);

		textField_remark = new JTextField();
		textField_remark.setBounds(88, 126, 125, 21);
		textField_remark.setColumns(10);
		contentPane.add(textField_remark);

		JLabel label_2 = new JLabel("数据表名");
		label_2.setBounds(223, 126, 66, 15);
		contentPane.add(label_2);

		textField_tableName = new JTextField();
		textField_tableName.setBounds(295, 123, 137, 21);
		textField_tableName.setColumns(10);
		contentPane.add(textField_tableName);

		JLabel label = new JLabel("包路径");
		label.setBounds(12, 154, 73, 15);
		contentPane.add(label);

		textField_codePath = new JTextField();
		textField_codePath.setBounds(88, 151, 125, 21);
		textField_codePath.setColumns(10);
		contentPane.add(textField_codePath);

		JLabel label_3 = new JLabel("模块排序");
		label_3.setBounds(223, 154, 66, 15);
		contentPane.add(label_3);

		textField_sortIndex = new JSpinner();
		textField_sortIndex.setBounds(295, 151, 137, 21);
		contentPane.add(textField_sortIndex);

		btn_saveInfo = new JButton("添加模块");
		btn_saveInfo.setBounds(76, 257, 127, 23);
		contentPane.add(btn_saveInfo);

		btn_delModule = new JButton("取消");
		btn_delModule.setBounds(272, 257, 115, 23);
		contentPane.add(btn_delModule);

		ViewUtil.toCenter(this);
	}

}
